#include "ScreenHelper.h"
#include <QMutex>
#include <QGuiApplication>
#include <QScreen>
#include <QScopedPointer>

ScreenHelper::ScreenHelper(QObject *parent)
    : QObject(parent)
    , ppi(QGuiApplication::primaryScreen()->physicalDotsPerInch())
    ,lpi(QGuiApplication::primaryScreen()->logicalDotsPerInch())
{

}

qreal ScreenHelper::dp2px(const qreal &size)
{
    return qRound(size * (ppi / lpi));
}

qreal ScreenHelper::px2dp(const qreal &px)
{
    return qRound(px / (ppi / lpi));
}

qreal ScreenHelper::px2pt(const qreal &px)
{
    return qRound(px * (ppi / lpi));
}


/* font pix */
qreal ScreenHelper::pt2px(const qreal &pt)
{
//#ifdef _WIN32
//    lpi = 90.0;
//#else
//    lpi = 160.0;
//#endif
    return qRound(pt * (ppi / lpi));
}

qreal ScreenHelper::sp2pt(const qreal &sp)
{
    return  sp;
}

qreal ScreenHelper::sp2px(const qreal &sp)
{
    return pt2px(sp2pt(sp));
}

ScreenHelper * ScreenHelper::instance = nullptr;

ScreenHelper *ScreenHelper::GetInstance()
{
    if(nullptr == instance)
    {
        instance = new ScreenHelper();
    }
    return instance;
}
